import { useUserInfoStore } from '@/stores/userInfo'
import { showToast } from 'vant'
import { createRouter, createWebHistory, useRouter } from 'vue-router'
import userApi from '@/api/userInfo'
const router = createRouter({
  history: createWebHistory(),
  routes: [
    // {
    //   path: '/ipt',
    //   name: 'Ipt',
    //   component: () => import('@/components/ipt/index.vue')
    // },
    {
      path: '/search',
      name: 'Search',
      component: () => import('@/views/search/index.vue'),
      meta: {
        hidden: true
      }
    },
    {
      path: '/',
      name: 'Index',
      component: () => import('@/views/index/index.vue'),
      meta: {
        hidden: true
      }
    },
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/catorylist',
      name: 'catorylist',
      component: () => import('@/views/catorylist/index.vue'),
      meta: {
        hidden: true
      }
    },
    {
      path: '/worth',
      name: 'worth',
      component: () => import('@/views/worth/index.vue'),
      meta: {
        hidden: true
      }
    },
    {
      path: '/shopCard',
      name: 'ShopCard',
      component: () => import('@/views/shopCard/index.vue'),
      meta: {
        hidden: true
      }
    },
    {
      path: '/center',
      name: 'Center',
      component: () => import('@/views/center/index.vue'),
      meta: {
        hidden: true
      }
    },
    {
      path: '/address',
      name: 'Address',
      component: () => import('@/views/address/index.vue')
    },
    {
      path: '/ordergoods',
      name: 'Ordergoods',
      component: () => import('@/views/ordergoods/index.vue'),
      meta: {
        hidden: true
      }
    },
    {
      path: '/loginCenter',
      name: 'LoginCenter',
      component: () => import('@/views/center/components/loginCenter/index.vue'),
      meta: {
        hidden: false
      }
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/center/components/login/index.vue'),
      meta: {
        hidden: false
      }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/center/components/register/index.vue'),
      meta: {
        hidden: false
      }
    },
    {
      path: '/list',
      name: 'list',
      component: () => import('@/views/catorylist/goodsList/index.vue')
    },
    {
      path: '/dital',
      name: 'dital',
      component: () => import('@/views/catorylist/detail/index.vue')
    },
    {
      path: '/pay',
      name: 'Pay',
      component: () => import('@/views/pay/index.vue')
    },
    {
      path: '/paysuccess',
      name: 'paysuccess',
      component: () => import('@/views/paysuccess/index.vue')
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  // 设置鉴权路由
  let whiteRoutes = ['/shopCard', '/center']
  const token = localStorage.getItem('token_key')

  // 判断是否存在token
  if (token) {
    // 存在token时查看是否存在用户信息
    let userInfo = useUserInfoStore().userInfo
    if (userInfo && userInfo.nickName) {
      next()
    } else {
      // 获取用户信息
      try {
        await useUserInfoStore().getUserInfo
        next()
        const res = await userApi.getUserInfo()
        if (res.code === 200) {
          useUserInfoStore().userInfo = res.data
        } else {
          showToast('获取用户信息失败')
        }
      } catch (error) {
        //若用户信息不存在，可能是token失效
        localStorage.removeItem('token_key')
        next('/login')
        showToast('用户信息已失效，请重新登录')
      }
    }
    next()
  } else {
    // 如果token不存在，做鉴权处理
      if (whiteRoutes.includes(to.path)) {
        // next(`login?direction:route.path`)
        router.push({
          path: '/login',
          query: { redirect: `${to.path}` }
        })
        next()
        // next('/login')
      } else {
        next()
      }
  }
})

export default router
